/**
 * Ingenius-Group S.A.S
 *
 * Project: SAU
 * File: EditDocumentView.java
 * Date: 04/02/2011
 */

package com.ingenius.sau.client.view;

import java.util.List;

import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.FormEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.form.FileUploadField;
import com.extjs.gxt.ui.client.widget.form.FormPanel.Encoding;
import com.extjs.gxt.ui.client.widget.form.FormPanel.Method;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.gwt.core.client.GWT;
import com.ingenius.sau.client.presenter.EditDocumentPresenter.EditDocumentDisplay;
import com.ingenius.sau.client.view.widgets.ListBox;
import com.ingenius.sau.shared.model.core.SelfEvaluation;

/**
 * Class <strong>EditDocumentView</strong>
 * 
 * @author Jesus David
 * @version 0.1
 */
public class EditDocumentView extends AbstractEditView implements
		EditDocumentDisplay {

	// UI Widgets
	private final TextField<String> textName;
	private final TextArea textDescription;
	private final ListBox comboProcess;
	private final FileUploadField uploadField;
	private String attachmentUrl;
	private String attachmentName;

	// Constructor

	public EditDocumentView() {
		super();
		textName = new TextField<String>();
		textDescription = new TextArea();
		comboProcess = new ListBox();
		uploadField = new FileUploadField();
		attachmentUrl = null;
		attachmentName = null;

		// TODO i18n
		setHeading("Edit Document");
		setSize(500, 330);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ingenius.sau.client.mvp.views.AbstractEditView#createComponents()
	 */
	@Override
	protected void createComponents() {
		// TODO i18n
		FormData formData = new FormData("-20");
		textName.setFieldLabel("Name");
		textName.setAllowBlank(false);
		textName.addPlugin(plugin);
		textName.setData("text", "Enter the document name");
		textName.getMessages().setBlankText("Name is required");

		textDescription.setFieldLabel("Description");
		textDescription.setHeight(75);
		textDescription.setAllowBlank(false);
		textDescription.addPlugin(plugin);
		textDescription.setData("text", "Any description is fine...");
		textDescription.getMessages().setBlankText("Description is required");

		comboProcess.setFieldLabel("Process");
		comboProcess.setAllowBlank(true);
		comboProcess.setEditable(false);
		comboProcess.addPlugin(plugin);
		comboProcess.setData("text", "Select the process owner");

		uploadField.setFieldLabel("Attachment");
		uploadField.setAllowBlank(false);
		uploadField.setName("uploadedfile");
		uploadField.addPlugin(plugin);
		uploadField.setData("text", "Select the document to upload");

		form.add(textName, formData);
		form.add(textDescription, formData);
		form.add(comboProcess, formData);
		form.add(uploadField, formData);

		registerEvents();
	}

	private void registerEvents() {
		form.setMethod(Method.POST);
		form.setAction(GWT.getModuleBaseURL() + "uploadService");
		form.setEncoding(Encoding.MULTIPART);
		form.addListener(Events.Submit, new Listener<FormEvent>() {
			@Override
			public void handleEvent(FormEvent fe) {
				attachmentUrl = "";
				attachmentName = "";
			}
		});
		form.addListener(Events.Complete, new Listener<FormEvent>() {
			@Override
			public void handleEvent(FormEvent fe) {
				attachmentUrl = fe.getResultHtml();
				System.out.println("FILE Client --->" + attachmentUrl);
			}
		});

		uploadField.addListener(Events.OnChange, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent be) {
				attachmentUrl = null;
				attachmentName = null;
				if (uploadField.getValue() != null) {
					form.submit();
				}
			}
		});
	}

	@Override
	public String getName() {
		return textName.getValue();
	}

	@Override
	public void setName(String name) {
		textName.setValue(name);
	}

	@Override
	public String getDescription() {
		return textDescription.getValue();
	}

	@Override
	public void setDescription(String description) {
		textDescription.setValue(description);
	}

	@Override
	public String getProcessId() {
		return comboProcess.getSimpleValue();
	}

	@Override
	public void setProcessId(String processId) {
		comboProcess.setSimpleValue(processId);
	}

	@Override
	public void setProcesses(List<SelfEvaluation> processes) {
		for (SelfEvaluation process : processes) {
			comboProcess.add(process.getId(), process.getName());
		}
	}

	@Override
	public String getAttachmentUrl() {
		return attachmentUrl;
	}

	@Override
	public void setAttachmentUrl(String url) {
		this.attachmentUrl = url;
	}

	@Override
	public String getAttachmentName() {
		return attachmentName;
	}

	@Override
	public void setAttachmentName(String name) {
		uploadField.setValue(name);
		this.attachmentName = name;
	}

	@Override
	protected boolean isValid() {
		return super.isValid() && ((attachmentUrl == null) ? false : true);
	}

}
